// pages/templates/camera/camera.js
import base from '../../../config/base.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    phflag: {
      type: Number,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    rootdata: {
      sgk: base.static + '/camera/sgk.png',
      sgg: base.static + '/camera/sgg.png',
      jtq: base.static + '/camera/jtq.png',
      xiangc: base.static + '/camera/xiangc.png',
      xj_box_p: base.static + '/camera/xj_box_p.png',
      xj_box_a: base.static + '/camera/xj_box_a.png',
      chongpai: base.static + '/camera/chongpai.png',
      shiyong: base.static + '/camera/shiyong.png',
      xj_back: base.static + '/camera/xj_back.png',
    },
    flashflag: 'off',
    detype: 'back',
    phflag: 0
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //隐藏相机
    hidephflag: function () {
      this.setData({
        phimg: ''
      });
      this.uploadparent();
    },
    //重拍
    loadphflag: function () {
      this.setData({
        phflag: 1
      });
    },
    //打开相册
    chooseImage() {
      this.setData({
        phflag: 0
      });
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
        success: (res) => {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          this.setData({
            phimg: tempFilePaths[0]
          });
          this.uploadparent();
        },
        fail: (res) => {
          this.setData({
            phflag: 1
          });
        },
      })
    },
    //闪光灯切换
    flashfn(){
      if (this.data.flashflag == 'off') {
        this.setData({
          flashflag: 'on'
        });
      } else {
        this.setData({
          flashflag: 'off'
        });
      }
    },
    //前后 摄像头切换
    detypefn() {
      if (this.data.detype == 'back') {
        this.setData({
          detype: 'front'
        });
      } else {
        this.setData({
          detype: 'back'
        });
      }
    },
    takephoto(){
      wx.createCameraContext().takePhoto({
        quality: 'normal',
        success: (res) => {
          this.setData({
            phimg: res.tempImagePath
          });
          console.log('takePhoto success', res);
        },
        fail: (res) => {
          console.log('fail', res);
        },
        complete: (res) => {
          this.setData({
            phflag: 2
          });
          console.log('complete', res);
        },
      })
    },
    phloadImg:function(e){
      console.log('拍照成功>>>>>>',e);
    },
    uploadparent:function(){
      var myEventDetail = {
        phimg: this.data.phimg,
        phflag: 0
      }; // detail对象，提供给事件监听函数
      var myEventOption = {}; // 触发事件的选项
      this.triggerEvent('cameraImg', myEventDetail, myEventOption) 
    },
  }
})
